package com.example.zwlyz.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zwlyz.R;
import com.example.zwlyz.models.CartItem;

import java.util.List;

public class CartAdapter extends RecyclerView.Adapter<CartAdapter.CartViewHolder> {
    private List<CartItem> cartItems;
    private OnCartItemClickListener listener;

    public CartAdapter(List<CartItem> cartItems, OnCartItemClickListener listener) {
        this.cartItems = cartItems;
        this.listener = listener;
    }

    @NonNull
    @Override
    public CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_cart, parent, false);
        return new CartViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CartViewHolder holder, int position) {
        final int pos = position;
        final CartItem cartItem = cartItems.get(position);
        holder.nameTextView.setText(cartItem.getName());
        holder.quantityTextView.setText(String.valueOf(cartItem.getQuantity()));
        holder.priceTextView.setText(String.format("￥%.2f", cartItem.getPrice() * cartItem.getQuantity()));

        holder.ivIncrease.setOnClickListener(v -> {
            cartItem.setQuantity(cartItem.getQuantity() + 1);
            holder.quantityTextView.setText(String.valueOf(cartItem.getQuantity()));
            holder.priceTextView.setText(String.format("￥%.2f", cartItem.getPrice() * cartItem.getQuantity()));
            listener.onCartItemChanged();
            notifyItemChanged(pos);
        });

        holder.ivDecrease.setOnClickListener(v -> {
            if (cartItem.getQuantity() > 0) {
                cartItem.setQuantity(cartItem.getQuantity() - 1);
                holder.quantityTextView.setText(String.valueOf(cartItem.getQuantity()));
                if (cartItem.getQuantity() == 0) {
                    cartItems.remove(pos);
                    notifyItemRemoved(pos);
                    listener.onCartItemChanged();
                } else {
                    holder.priceTextView.setText(String.format("￥%.2f", cartItem.getPrice() * cartItem.getQuantity()));
                    listener.onCartItemChanged();
                    notifyItemChanged(pos);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return cartItems.size();
    }

    public void setCartItems(List<CartItem> cartItems) {
        this.cartItems = cartItems;
        notifyDataSetChanged();
    }

    static class CartViewHolder extends RecyclerView.ViewHolder {
        TextView nameTextView;
        TextView quantityTextView;
        TextView priceTextView;
        ImageView ivIncrease;
        ImageView ivDecrease;

        CartViewHolder(View itemView) {
            super(itemView);
            nameTextView = itemView.findViewById(R.id.nameTextView);
            quantityTextView = itemView.findViewById(R.id.quantityTextView);
            priceTextView = itemView.findViewById(R.id.priceTextView);
            ivIncrease = itemView.findViewById(R.id.ivIncrease);
            ivDecrease = itemView.findViewById(R.id.ivDecrease);
        }
    }

    public interface OnCartItemClickListener {
        void onCartItemChanged();
    }
}